<?php
/**
 * created by zzy
 * date: 2017/11/28 17:36
 */


namespace App\Api\Service\Order\Controllers;

use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\OrderPre;
use App\Engine\Func;

class OrderPreController extends Controller
{
    public function lists()
    {

        $userId        = Func::getHeaderValueByName( 'userid' );
        $info           = DepartmentUser::getCurrentInfo( $userId );
        $companyId     = $info->company_id;

        $page           = request( 'page', 1 );
        $limit          = request('limit', 10 );
        $offset         = ( $page - 1 ) * $limit;

        $obj            = OrderPre::where('uid','=',$userId);
        $obj            = $obj->where('ygt_order_pre.company_id','=', $companyId);
        $obj            = $obj->leftJoin('ygt_order_type','ygt_order_type.id','=','ygt_order_pre.order_type');
        $obj            = $obj->select( ['ygt_order_pre.id','ygt_order_pre.customer_order_id','ygt_order_pre.order_type','ygt_order_type.title','ygt_order_pre.created_at'] );
        $obj            = $obj->skip( $offset );
        $obj            = $obj->take( $limit );
        $obj            = $obj->orderBy( 'id','desc' );
        $data           = $obj->get()->toArray();
        foreach( $data as $key=>$val ){
            $created_at_ymd               = date('Y-m-d',$val['created_at']);
            $data[$key]['created_at']   = $created_at_ymd;
            $data[$key]['customer_order_id'] = $data[$key]['customer_order_id'] ? $data[$key]['customer_order_id'] : '';
        }
        $obj            = OrderPre::where('uid','=',$userId);
        $obj            = $obj->where('company_id','=', $companyId);
        $count          = $obj->count();
        $pagingData     = [
            'page'         => $page,
            'pageSize'     => $limit,
            'total'        => $count,
            'keyword'       => ''
        ];
        return $this->pagingData($data, $pagingData);
    }
    public function delete()
    {
        $userId            = Func::getHeaderValueByName( 'userid' );
        $id                 = request( 'id', 0 );
        if( !$id ){
            xThrow( 10006 );
        }
        if( $id === 'all' ){
            $delResult         = OrderPre::where('uid','=', $userId)->delete();
        } else {
            $idArr             = explode( ',', $id );;
            $delResult         = OrderPre::where('uid','=', $userId)->whereIn('id',$idArr)->delete();
        }
        if( $delResult === false ){
            xThrow( 99999 );
        }
        $result         = ['message'=>'删除成功'];
        return $result;
    }

    public function customerOrderPreList()
    {

        $userId        = Func::getHeaderValueByName( 'userid' );
        $info           = DepartmentUser::getCurrentInfo( $userId );
        $companyId     = $info->company_id;


        $page           = request( 'page', 1 );
        $limit          = request('limit', 10 );
        $offset         = ( $page - 1 ) * $limit;

        if($companyId == 2){
            //如果是客商
            $customerIdArr = \App\Eloquent\Ygt\Customer::where(['user_id'=>$userId])->get()->pluck('id');
            $obj            = \App\Eloquent\Ygt\CustomerOrderPre::whereIn('customer_name',$customerIdArr);

        }else{
            $obj            = \App\Eloquent\Ygt\CustomerOrderPre::where('uid','=',$userId);
            $obj            = $obj->where('ygt_customer_order_pre.company_id','=', $companyId);
        }


        $obj            = $obj->leftJoin('ygt_order_type','ygt_order_type.id','=','ygt_customer_order_pre.order_type');
        $obj            = $obj->select( ['ygt_customer_order_pre.id','ygt_customer_order_pre.order_type','ygt_order_type.title','ygt_customer_order_pre.created_at'] );
        $obj            = $obj->skip( $offset );
        $obj            = $obj->take( $limit );
        $obj            = $obj->orderBy( 'id','desc' );
        $data           = $obj->get()->toArray();
        foreach( $data as $key=>$val ){
            $created_at_ymd               = date('Y-m-d',$val['created_at']);
            $data[$key]['created_at']   = $created_at_ymd;
        }
        $obj            = \App\Eloquent\Ygt\CustomerOrderPre::where('uid','=',$userId);
        $obj            = $obj->where('company_id','=', $companyId);
        $count          = $obj->count();
        $pagingData     = [
            'page'         => $page,
            'pageSize'     => $limit,
            'total'        => $count,
            'keyword'       => ''
        ];
        return $this->pagingData($data, $pagingData);
    }

    public function customerOrderPreDelete()
    {
        $userId            = Func::getHeaderValueByName( 'userid' );
        $id                 = request( 'id', 0 );
        if( !$id ){
            xThrow( 10006 );
        }
        if( $id === 'all' ){
            $delResult         = \App\Eloquent\Ygt\CustomerOrderPre::where('uid','=', $userId)->delete();
        } else {
            $idArr             = explode( ',', $id );;
            $delResult         = \App\Eloquent\Ygt\CustomerOrderPre::where('uid','=', $userId)->whereIn('id',$idArr)->delete();
        }
        if( $delResult === false ){
            xThrow( 99999 );
        }
        $result         = ['message'=>'删除成功'];
        return $result;
    }
}